package cn.iocoder.yudao.module.crm.mq.consumer.inittenant;

import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.crm.service.followup.CrmFollowStageConfigService;
import cn.iocoder.yudao.module.crm.service.followup.CrmFollowTypeConfigService;
import cn.iocoder.yudao.module.crm.service.label.CrmLabelService;
import cn.iocoder.yudao.module.crm.service.levelconfig.CrmLevelConfigService;
import cn.iocoder.yudao.module.crm.service.module.CrmModuleService;
import cn.iocoder.yudao.module.system.mq.message.inittenant.InitTenantMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 针对 {@link InitTenantMessage} 的消费者
 *
 * @author PetePower
 */
@Component
@Slf4j
public class CrmInitTenantConsumer {

    @Resource
    private CrmLevelConfigService crmLevelConfigService;

    @Resource
    private CrmLabelService crmLabelService;

    @Resource
    private CrmFollowTypeConfigService crmFollowTypeConfigService;

    @Resource
    private CrmFollowStageConfigService crmFollowStageConfigService;

    @Resource
    private CrmModuleService crmModuleService;

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(InitTenantMessage message) {
        TenantUtils.execute(message.getTenantId(), () -> {
            crmModuleService.initModule();
            crmLabelService.initLabel();
            crmLevelConfigService.initLeveConfig();
            crmFollowTypeConfigService.initFollowTypeConfig();
            crmFollowStageConfigService.initFollowStageConfig();
        });
    }

}
